package cn.itsource.basic.controller;

import cn.itsource.basic.dto.CodeDto;
import cn.itsource.basic.dto.EmaDto;
import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.service.IVerifyCodeService;
import cn.itsource.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public AjaxResult imageVerifyCode(@PathVariable("key") String key){

        try {
            String base64Str= verifyCodeService.imageVerifyCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试");
        }


        //也可以直接返回 第一次思路
        //return verifyCodeService.imageVerifyCode(key);
    }


    /**
     * 获取手机验证码接口
     * @param
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult sendSmsCode(@RequestBody CodeDto codeDto){
        try {
            verifyCodeService.phoneVerifyCode(codeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试");
        }
    }

    /**
     * 获取邮箱验证码接口
     * @param
     * @return
     */
    @PostMapping("/emailCode")
    public AjaxResult emailCode(@RequestBody EmaDto emaDto){
        try {
            verifyCodeService.emailCode(emaDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试");
        }
    }

    @PostMapping("/binderSmsCode")
    public AjaxResult binderVerifyCode(@RequestBody CodeDto codeDto){
        try {
            verifyCodeService.binderVerifyCode(codeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试");
        }
    }
}
